package com.my.internetplus.controller;

import com.my.internetplus.model.Good;
import com.my.internetplus.result.BaseResult;
import com.my.internetplus.service.GoodService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;

@RestController
public class GoodController {

    @Resource
    private GoodService goodService;

    @RequestMapping("selectGoodsList")
    public BaseResult<List<Good>> selectGoodsList(int goodtype){
        return BaseResult.success(goodService.selectGoodsList(goodtype));
    }
    public static String ImageToBase64(String imgPath) {
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过的字节数组字符串
       return  encoder.encode(Objects.requireNonNull(data));

    }
    @RequestMapping("getPic")
    public String getPic(){
       return ImageToBase64("E:\\abcd\\zz.png");

    }
}
